1 module hunt.templates.cache;
2 
3 import std.stdio;
4 import std.array;
5 import std.string;
6 import core.sync.rwmutex;
7 import std.digest.sha;
8 import std.digest.md;
9 import hunt.templates.ast;
10 
11 class ASTCacheManager
12 {
13 
14     ASTNode node(string path)
15     {
16         synchronized (_mutex.reader)
17         {
18             // writeln("----cache hit ast node : ",path);
19             return _astMap.get(path, null);
20         }
21 
22     }
23 
24     void add(string path, ASTNode node)
25     {
26         _mutex.writer.lock();
27         scope (exit)
28             _mutex.writer.unlock();
29 
30         auto ast = _astMap.get(path, null);
31         if (ast is null)
32         {
33             //writeln("add cache : ",path," key : ",path," cache size: ",_astMap.length);
34             _astMap[path] = node;
35         }
36 
37         // writeln(" cache keys : ",_astMap.keys);
38     }
39 
40 private:
41     this()
42     {
43         _mutex = new ReadWriteMutex();
44     }
45 
46     ~this()
47     {
48         _mutex.destroy;
49     }
50 
51     ASTNode[string] _astMap;
52 
53     ReadWriteMutex _mutex;
54 }
55 
56 @property ASTCacheManager ASTCache()
57 {
58     return _astcache;
59 }
60 
61 shared static this()
62 {
63     //writeln("#######rrt");
64     _astcache = new ASTCacheManager();
65 }
66 
67 private:
68 __gshared ASTCacheManager _astcache;